#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include"stu.h"
struct Node* creatList()
{
	struct Node* listHeadNode = (struct Node*)malloc(sizeof(struct Node));
	listHeadNode->next=NULL;
	return listHeadNode; 
}
struct Node* creatNode(struct student data)
{
	struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
	newNode->data=data;
	newNode->next=NULL;
	return newNode; 
}
//查找
struct Node* searchNodeByAppoinNum(struct Node* listHeadNode,char *num)
{
	struct Node* pMove=listHeadNode->next;
	if(pMove==NULL)
		return pMove;
	else
	{
		while(strcmp(pMove->data.num,num))
		{
			pMove=pMove->next;
			if(pMove==NULL)
				break;
		}
		return pMove;
	}
}
void printNode(struct Node* curNode)
{
	printf("姓名\t学号\t已选课程\t总计分数\n");
	printf("%s\t%s\t%s\t%s\n",curNode->data.name,curNode->data.num,curNode->data.lesson,curNode->data.score);
}
//修改

//删除
void deleteNodeByAppoinName(struct Node* listHeadNode,char *name)
{
	struct Node* posFrontNode=listHeadNode;
	struct Node* posNode=listHeadNode->next;
	if(posNode==NULL)
	{
		printf("无相关内容,无法删除\n");
		return;
	}else{
		while(strcmp(posNode->data.name,name))
		{	
			posFrontNode=posNode;
			posNode=posFrontNode->next;
			if(posNode==NULL)
			{
				printf("无相关内容,无法删除\n");
				return;
			}	
		}
		posFrontNode->next=posNode->next;
		free(posNode);
	}
} 
//插入 
void insertNodeByHead(struct Node* listHeadNode, struct student data)
{
	struct Node* newNode=creatNode(data);
	newNode->next=listHeadNode->next;
	listHeadNode->next=newNode;
}
//打印 
void printList(struct Node* listHeadNode)
{
	struct Node* pMove =listHeadNode->next;
	printf("姓名\t学号\t已选课程\t总计分数\n");
	while(pMove)
	{
		printf("%s\t%s\t%s\t%s\n",pMove->data.name,pMove->data.num,pMove->data.lesson,pMove->data.score);
		pMove=pMove->next; 
	}
	printf("\n");
}

void readInfoFromFile1(char *fileName,struct Node* listHeadNode)
{
	FILE *fp=fopen(fileName,"r");
	if(fp==NULL)
	{
		fp=fopen(fileName,"w");	
	}	
	struct student tempData;
	while(fscanf(fp,"%s\t%s\t%s\t%s\n",tempData.name,tempData.num,tempData.lesson,tempData.score)!=EOF)
	{
		insertNodeByHead(listHeadNode,tempData);
		memset(&tempData,0,sizeof(tempData));
	}
	fclose(fp);
}
void saveInfoToFile(char  *fileName,struct Node* listHeadNode)
{
	FILE *fp=fopen(fileName,"w");
	struct Node* pMove =listHeadNode->next;
	while(pMove)
	{
		fprintf(fp,"%s\t%s\t%s\t%s\n",pMove->data.name,pMove->data.num,pMove->data.lesson,pMove->data.score);
		pMove=pMove->next; 
	}
	fclose(fp);
}
